Strings - class that represents character strings. Strings are an ordered sequence of characters. They are for visual purposes only, displaying text such as, "Meermans08". Strings are counted both as a primitive type and a class unto itself.

When creating a string, declare a variable to be a referance to an object. For example,

String sentence;
The variable sentence is declared as a reference to a String object. Afterwards, create (a.k.a. instantiate) it using the new operator. For example,

String sentence = new String("Computer Science is fun!");
                          OR
String sentence = "Computer Science is fun!";   <----simple
 
After the string is created, you can do many things with it using different methods. For example,
int sentenceLength = sentence.length();
//returns the length of the string and sets it into the integer sentenceLength
 
sentence = sentence.toUpperCase();
//makes the string all upper case
String low = sentence.toLowerCase();
//makes another string where the initial string is all lower case
 
String valentine = new String("Valentine's Day ");
String not = new String("is not a real holiday.");
String together = valentine.concat(not);
System.out.print(together);
//concatenates (adds) the string not at the end of the string valentine
//prints Valentine's Day is not a real holiday.
 
String Creation(2 ways):examples
- String annoying Sound = newString("Pika-chu");
- String annoying Sound = "Pika-chu";
 
 

String Methods

(int) .compareTo (some string to compare against)
returns:
a negative number if 1st is alphabetically before 2nd, a 0 if 1st and 2nd are equal, and a positive if 1st is alphabetically after 2nd.
explanation:
It's good for sorting names or things from one another. Although you cannot say if one is less than or greater than another in an if-statement (if "pika-chu" < "raichu"), you can say if one equals one another in an if-statement (if "pika-chu" == "raichu").
examples:
If String rawr = "dinosaur"; is your String, then answer the following ...
1. rawr.compareTo ("koala");
answer: -1
Since "dinosaur" comes before "koala" in the alphabet then you would return a negative number, which is most likely -1.
2. rawr.compareTo ("aardvark");
answer: 1
Since "dinosaur" comes after "aardvark" in the alphabet then you would return a positive number, which is most likely 1.
3. rawr.compareTo("dinosaur");
answer:
0
Since "dinosaur" is equal to "dinosaur" in the string, then you would return a 0.

(boolean) .equals (some string to compare against)
returns:
TRUE if 2 strings are EQUAL, returns FALSE if 2 strings are UNEQUAL
explanation:
Compares to see if two strings equal one another, almost like an alternative for if(myVariable.equals("pika-chu")). It DOES matter though if there are capital and lowercase letters involved.
examples:
If String rawr = "dinosaur"; is your String, then answer the following ...
1. rawr.equals ("koala");
answer: false
Since "koala" does not equal "dinosaur", then you would return false.
2. rawr.equals ("dinosaur");
answer: true
Since "dinosaur" equals "dinosaur", then you would return true.

(int) .indexOf (some string to search for)
returns:
the index(position) of the beginning of the string searched for; returns -1 if not found
explanation:
For String rawr = "dinosaur"; the method .indexOf could ask rawr.indexOf("dino"); which would be asking if "dino" is in your word "dinosaur". Since it found "dino" in "dinosaur", then it will return the slot number of the beginning of the string "dino", which would be 0. But if, for example, you were trying to find "dino-saur" or even "dino saur" then the method .indexOf would return a -1 because it was unable to locate the ENTIRE string you asked for. In other situations where certain phrases are repeated in the string such as the "iss" in "Mississippi", if you are asking for stateName.indexOf("iss"), then it will only return the slot number of the first "iss" found, which would be 1.
examples:
If String rawr = "dinosaur"; is your String, then answer the following ...
1. rawr.indexOf ("koala");
answer: -1
Since "koala" is not found in "dinosaur", then you would return a -1.
2. rawr.indexOf ("saur");
answer: 4
Since "dinosaur" was found in "dinosaur", then you would return the beginning of the string "saur", which is 4.


(int) .length()
returns:
the number if characters in the string
explanation:
If you had String rawr = "dinosaur"; then the method .length could ask rawr.length() which would return 8 because there are 8 slot numbers in the string "dinosaur". Remember the parenthesis at the end, there was an error that said that the .length method does not require parenthesis, this is false.
examples:
If String rawr = "bubbles"; is your String, then answer the following ...
1. rawr.length ();
answer: 7
Since there are 7 characters in the string, you would return 7.

(string) .substring (start index)
returns:
a string starting at index and going to the end
explanation:
If you had String rawr = "dinosaur"; then the method .substring could ask rawr.substring(4). The method would then go to the 4th slot and chop off the rest of the string, returning "saur".
examples:
If String rawr = "dinosaur"; is your String, then answer the following ...
1. rawr.substring (2);
answer: nosaur
Since the 2nd slot in "dinosaur" is n, everything after slot 2 would be cut, leaving you with "nosaur".

(string) .substring (start index, end index)
returns:
a string starting at 1st number and ending at 2nd number
explanation:
If you had String rawr = "dinosaur"; then the method .substring could ask rawr.substring(1, 3);. The method would then go to the starting point which is 1 and cut off the "d" leaving you "inosaur". Then the method would go to the ending point which is 3 and chop off the rest of the string INCLUDING the ending point which is "osaur". The method would return "in". In other situations, the method .substring could ask rawr.substring(3).substring(2);. The method would go to the 3rd slot, and chop off the string leaving you with "osaur" as the new string. Then the method would go to the 2nd slot of "osaur" and chop off the rest of the string, leaving you with "aur".
examples:
If String rawr = "dinosaur"; is your String, then answer the following ...
1. rawr.substring (3,6);
answer: osa
Everything starting from slot 3 and BEFORE slot 6 would be cut, so slots 3, 4, and 5 would be cut off, leaving you with osa.




ex.
String x = "carcassonne";
 
   int a = x.length();                    //a = 11
   int b = x.indexOf("son");              //b = 6
   int c = x.compareTo("blokus");         //c = +1
   string d = x.substring(4,8);           //d = asso
   boolean e = x.equals("carcassonne");   //e = true